home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 / SpriteWorld Examples / Stars / Stars.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-25  |  8.5 KB  |  323 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. // Stars.c
  3. //
  4. // By Vern Jensen. Created 9/2/96
  5. ///--------------------------------------------------------------------------------------
  6.  
  7.  
  8. #include <SWFPSReport.h>
  9. #include <SWIncludes.h>
  10. #include <SWGameUtils.h>
  11.  
  12. #include "SWApplication.h"
  13. #include "Stars.h"
  14.  
  15.  
  16. #define    kFullScreenWindow            true        // Try me full screen!
  17. #define kWorldRectInset                0            // Make the SpriteWorld smaller?
  18. #define kSyncToVBL                    true        // Sync SpriteWorld to VBL?
  19. #define kMaxFPS                        0            // Set to 0 for unrestricted speed
  20.  
  21. #define kNumStars                    150
  22. #define kMinStarSpeed                1
  23. #define kMaxStarSpeed                3
  24.  
  25. #define kNumSprites                    2
  26. #define    kBaseSpriteMoveDelta        2
  27.  
  28.  
  29. ///--------------------------------------------------------------------------------------
  30. // Globals
  31. ///--------------------------------------------------------------------------------------
  32.  
  33. SpriteWorldPtr    gSpriteWorldP;
  34. WindowPtr        gWindowP;
  35. StarArray        gStarArray[kNumStars];
  36.  
  37. SpritePtr        gSimpleSpriteArray[kNumSprites];
  38.  
  39.  
  40. ///--------------------------------------------------------------------------------------
  41. // Main
  42. ///--------------------------------------------------------------------------------------
  43.  
  44. void    main( void )
  45. {
  46.     Initialize(kNumberOfMoreMastersCalls);
  47.     Randomize();
  48.     
  49.     if ( SWHasSystem7() )
  50.     {
  51.         AllowKeyUpEvents();    // Part of SWGameUtils.c
  52.         SetCursor(*GetCursor(watchCursor));
  53.         
  54.         CreateSpriteWorld();
  55.         CreateSprites();
  56.         
  57.         SetCursor(&qd.arrow);
  58.         HideCursor();
  59.         
  60.         RunAnimation();
  61.         ShutDown();
  62.         
  63.         RestoreEventMask();    // Make sure to call this after calling AllowKeyUpEvents
  64.     }
  65.     else
  66.     {
  67.         CantRunOnThisMachine();
  68.     }
  69. }
  70.  
  71.  
  72. ///--------------------------------------------------------------------------------------
  73. // CreateSpriteWorld
  74. ///--------------------------------------------------------------------------------------
  75.  
  76. void    CreateSpriteWorld( void )
  77. {
  78.     Rect        worldRect, windRect;
  79.     RgnHandle    mBarUpdateRgn;
  80.     OSErr        err;
  81.     
  82.     gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
  83.     
  84.     if (gWindowP != NULL)
  85.     {
  86.         if ( GetDepthFromWindow(gWindowP) != 8 )
  87.         {
  88.             SetCursor(&qd.arrow);
  89.             StopAlert(130, NULL);
  90.             ExitToShell();
  91.         }
  92.         
  93.         
  94.         if (kFullScreenWindow == true)
  95.         {
  96.             SizeWindow(gWindowP, qd.screenBits.bounds.right, 
  97.                     qd.screenBits.bounds.bottom, false);
  98.             MoveWindow(gWindowP, 0, 0, false);
  99.         }
  100.         else
  101.         {
  102.                 // Center window in screen
  103.             windRect = gWindowP->portRect;
  104.             CenterRect(&windRect, &qd.screenBits.bounds);
  105.             MoveWindow(gWindowP, windRect.left, windRect.top, false);
  106.         }
  107.         
  108.         ShowWindow(gWindowP);
  109.         SetPort(gWindowP);
  110.         mBarUpdateRgn = SWHideMenuBar(gWindowP); // Must be done *after* showing window!
  111.         EraseRgn(mBarUpdateRgn);
  112.         
  113.         ForeColor(blackColor);
  114.         BackColor(whiteColor);
  115.     }
  116.     else
  117.         CantFindResource();
  118.     
  119.     
  120.     err = SWEnterSpriteWorld();
  121.     FatalError(err);
  122.     
  123.     
  124.     worldRect = gWindowP->portRect;
  125.     InsetRect(&worldRect, kWorldRectInset, kWorldRectInset);
  126.     
  127.     
  128.         // Create the sprite world
  129.     err = SWCreateSpriteWorldFromWindow(&gSpriteWorldP, (CWindowPtr)gWindowP, 
  130.             &worldRect, NULL, 0);
  131.     FatalError(err);
  132.     
  133.     
  134.     SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
  135.     SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
  136.  
  137.     
  138.     SWSetSpriteWorldMaxFPS(gSpriteWorldP, kMaxFPS);
  139.     SWSyncSpriteWorldToVBL(gSpriteWorldP, kSyncToVBL);
  140.     SWSetCleanUpSpriteWorld(gSpriteWorldP);
  141. }
  142.     
  143.  
  144. ///--------------------------------------------------------------------------------------
  145. // CreateSprites
  146. ///--------------------------------------------------------------------------------------
  147.  
  148. void    CreateSprites( void )
  149. {
  150.     SpriteLayerPtr        spriteLayerP;
  151.     SpritePtr            simpleSpriteP;
  152.     short                spriteNum;
  153.     OSErr                err;
  154.     
  155.     
  156.     err = SWCreateSpriteLayer(&spriteLayerP);
  157.     FatalError(err);
  158.  
  159.     err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &simpleSpriteP, NULL, 
  160.             128, 1, kFatMask);
  161.     FatalError(err);
  162.     
  163.     if (gSpriteWorldP->pixelDepth == 8)
  164.         SWCompileSprite(simpleSpriteP);
  165.     
  166.     gSimpleSpriteArray[0] = simpleSpriteP;
  167.  
  168.     for (spriteNum = 1; spriteNum < kNumSprites; spriteNum++)
  169.     {
  170.         err = SWCloneSprite(simpleSpriteP, &gSimpleSpriteArray[spriteNum], NULL);
  171.         FatalError(err);
  172.     }
  173.     
  174.  
  175.     for (spriteNum = 0; spriteNum < kNumSprites; spriteNum++)
  176.     {
  177.         simpleSpriteP = gSimpleSpriteArray[spriteNum];
  178.         
  179.         SWAddSprite(spriteLayerP, simpleSpriteP);
  180.         SWSetSpriteMoveBounds(simpleSpriteP, &gSpriteWorldP->backRect);
  181.         SWSetSpriteMoveProc(simpleSpriteP, BallSpriteMoveProc);
  182.         SWSetSpriteMoveDelta(simpleSpriteP, kBaseSpriteMoveDelta + spriteNum, 
  183.             kBaseSpriteMoveDelta + spriteNum);
  184.         SWSetSpriteLocation(simpleSpriteP, spriteNum * 60, spriteNum * 60);
  185.         SWSetSpriteDrawProc(simpleSpriteP, CompiledSprite8BitDrawProc);
  186.     }
  187.  
  188.     SWAddSpriteLayer(gSpriteWorldP, spriteLayerP);
  189.     SWLockSpriteWorld(gSpriteWorldP);
  190.     
  191.     SWSetPortToBackground(gSpriteWorldP);
  192.     PaintRect(&gSpriteWorldP->backRect);
  193. }
  194.  
  195.  
  196. ///--------------------------------------------------------------------------------------
  197. //  RunAnimation
  198. ///--------------------------------------------------------------------------------------
  199.  
  200. void    RunAnimation( void )
  201. {
  202.     unsigned long        frames;
  203.     
  204.     SetUpStars();
  205.     SWUpdateSpriteWorld(gSpriteWorldP, true);
  206.     SWAnimate8BitStarField(gSpriteWorldP, gStarArray, kNumStars, 255);
  207.     
  208.     frames = 0;
  209.     StartTimer();
  210.     
  211.     FatalError( SWStickyError() ); // Make sure no errors got past us during setup
  212.     
  213.     while (!Button())
  214.     {
  215.         SWProcessSpriteWorld(gSpriteWorldP);
  216.         FatalError( SWStickyError() );    // Make sure no errors occurred during a MoveProc, etc.
  217.         SWAnimateSpriteWorld(gSpriteWorldP);
  218.         
  219.         if (gSpriteWorldP->frameHasOccurred)
  220.         {
  221.             MoveStars();
  222.             SWAnimate8BitStarField(gSpriteWorldP, gStarArray, kNumStars, 255);
  223.             
  224.             frames++;
  225.         }
  226.     }
  227.     
  228.     
  229.     SWShowMenuBar(gWindowP);
  230.     ShowResults(frames);
  231. }
  232.  
  233.  
  234. ///--------------------------------------------------------------------------------------
  235. //  ShutDown (clean up and dispose of the SpriteWorld)
  236. ///--------------------------------------------------------------------------------------
  237.  
  238. void    ShutDown( void )
  239. {
  240.     SWDisposeSpriteWorld(&gSpriteWorldP);
  241.     SWExitSpriteWorld();
  242.     
  243.     FlushEvents(everyEvent, 0);
  244.     ShowCursor();
  245. }
  246.  
  247.  
  248. ///--------------------------------------------------------------------------------------
  249. //  BallSpriteMoveProc
  250. ///--------------------------------------------------------------------------------------
  251.  
  252. SW_FUNC void    BallSpriteMoveProc(SpritePtr ballSpriteP)
  253. {    
  254.     SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
  255.     (void)SWBounceSprite(ballSpriteP);
  256. }
  257.  
  258.  
  259. ///--------------------------------------------------------------------------------------
  260. //  SetUpStars
  261. ///--------------------------------------------------------------------------------------
  262.  
  263. void    SetUpStars( void )
  264. {    
  265.     short    curStar;
  266.     Rect    windRect = gSpriteWorldP->windowFrameP->frameRect;
  267.     
  268.     for (curStar = 0; curStar < kNumStars; curStar++)
  269.     {
  270.         gStarArray[curStar].horizLoc = GetRandom(windRect.left, windRect.right-1);
  271.         gStarArray[curStar].vertLoc = GetRandom(windRect.top, windRect.bottom-1);
  272.         gStarArray[curStar].oldHorizLoc = gStarArray[curStar].horizLoc;
  273.         gStarArray[curStar].oldVertLoc = gStarArray[curStar].vertLoc;
  274.         gStarArray[curStar].horizSpeed = GetRandom(kMinStarSpeed, kMaxStarSpeed);
  275.         gStarArray[curStar].color = 0;
  276.         
  277.             // Uncomment this line to make the stars different colors
  278. //        gStarArray[curStar].color = GetRandom(0, 254);
  279.  
  280. /*
  281.             // Uncomment this to make the stars different brightnesses
  282.         gStarArray[curStar].color = GetRandom(244, 254);
  283.         if ( gStarArray[curStar].color == 244 )
  284.             gStarArray[curStar].color = 0;
  285. */
  286.             // Since the star hasn't been drawn yet, it doesn't need to be erased.
  287.         gStarArray[curStar].needsToBeErased = false;
  288.     }
  289. }
  290.  
  291.  
  292. ///--------------------------------------------------------------------------------------
  293. //  MoveStars
  294. ///--------------------------------------------------------------------------------------
  295.  
  296. void    MoveStars( void )
  297. {    
  298.     short    curStar;
  299.     Rect*    windRectP = &gSpriteWorldP->windowFrameP->frameRect;
  300.     
  301.     for (curStar = 0; curStar < kNumStars; curStar++)
  302.     {
  303.             // Save the old position
  304.         gStarArray[curStar].oldHorizLoc = gStarArray[curStar].horizLoc;
  305.         gStarArray[curStar].oldVertLoc = gStarArray[curStar].vertLoc;
  306.         
  307.         gStarArray[curStar].horizLoc += gStarArray[curStar].horizSpeed;
  308.         if (gStarArray[curStar].horizLoc > windRectP->right)
  309.         {
  310.             gStarArray[curStar].vertLoc = GetRandom( windRectP->top, windRectP->bottom-1 );
  311.             gStarArray[curStar].horizLoc -= (windRectP->right - windRectP->left);
  312.         }
  313.  
  314. /*        
  315.             // Change star color (try uncommenting this for a neat effect)
  316.         gStarArray[curStar].color++;
  317.         if (gStarArray[curStar].color >= 255)
  318.             gStarArray[curStar].color -= 255;
  319. */
  320.     }
  321. }
  322.  
  323.